# from rest_framework.generics import ListCreateAPIView
# from rest_framework.pagination import LimitOffsetPagination
# from rest_framework import serializers
# from rest_framework.filters import BaseFilterBackend
# from api import models
#
#
# class TopicSerializer(serializers.ModelSerializer):
#     class Meta:
#         model=models.Topic
#         fields='__all__'
#
# class TopicFilterBackend(BaseFilterBackend):
#
#     def filter_queryset(self, request, queryset, view):
#         min_id=request.query_params.get('min')
#         if min_id:
#             return queryset.filter(id__gt=min_id)
#         else:
#             return queryset
#
# class TopicPagination(LimitOffsetPagination):
#
#     default_limit = 10
#     max_limit = 50
#     limit_query_param = 'limit'
#     offset_query_param = 'offset'
#
# class TopicAPiVIew(ListCreateAPIView):
#
#     queryset = models.Topic.objects.all()
#     serializer_class = TopicSerializer
#     pagination_class = TopicPagination
#     filter_backends = [TopicFilterBackend,]

from rest_framework.generics import ListAPIView
from rest_framework.generics import CreateAPIView
from rest_framework.serializers import ModelSerializer
from rest_framework.filters import BaseFilterBackend
from rest_framework.pagination import LimitOffsetPagination
from api import models

class TopicBaseFilterBackend(BaseFilterBackend):
    '''
    处理get请求参数
    '''
    def filter_queryset(self, request, queryset, view):
        min = request.query_params.get('min')
        if min:
            return queryset.filter(id__gt=min)
        else:
            return queryset

class TopicPagination(LimitOffsetPagination):
    default_limit = 5
    max_limit = 50
    limit_query_param = 'limit'
    offset_query_param = 'offset'

    def get_offset(self, request):
        return 0

class TopicListModelSerializer(ModelSerializer):

    class Meta:
        model=models.Topic
        fields='__all__'


class TopicApiView(ListAPIView,CreateAPIView):
    queryset = models.Topic.objects
    serializer_class = TopicListModelSerializer
    filter_backends = [TopicBaseFilterBackend,]
    pagination_class = TopicPagination